from flask import Blueprint, request
from conn_mysql import ConnectMysql
from .access_token_notify import access_token_verify_func
from setting import *

app_order_detail = Blueprint('app_order_detail', 'api')

@app_order_detail.route('/app_order_detail/<int:ID>')
def order_detail(ID):
    #接口 没有登录状态验证
    sql = "SELECT * FROM call_car WHERE id=%s"
    

    with ConnectMysql() as c:
        #这里最好是一次性提出来，两个表可以设置关第可以设置视图
        result = c.select_one(sql, (ID,))
      

    if not result:

        return {
            "status": -1,
            "errorCode": 71,
            "errorMsg" : "没有找到相应ID的订单",
            "resultBody": None
        }


    result['regedate'] = result['regedate'].__str__()
    result['go_time'] = result['go_time'].__str__()
    
    if result['carpooling_HitL'] == 0:
        result['carpooling_HitL'] = "拼车"
    else:
        result['carpooling_HitL'] = "包车"
    
    if result['direction'] == 0:

        result['direction'] = f'{start_station} 到 {end_station}'
    else:

        result['direction'] = f'{end_station} 到 {start_station}'

    
    with ConnectMysql() as c:
        sql_ = "SELECT * FROM order_status WHERE order_id=%s"
        
        result_order_status = c.select_one(sql_, (ID,))
        if result_order_status == None:
            result_order_status = {'order_status': '未处理'}
        else:
            order_status = result_order_status['order_status']
            if order_status == 0:
                result_order_status['order_status'] = '管理员取消该定单'
            elif order_status == 1:
                result_order_status['order_status'] = '已处理'
            elif order_status == 2:
                 result_order_status['order_status'] = '用户取消该定单'
            elif order_status == 3:
                 result_order_status['order_status'] = '用户操作该定单'

    
    # 如果果result_order_status为空，那么一定是未处理，
    # 如查result_order_status有值且result_order_status.get('driver_id')有值，
    # 后台管理员正常处理，order_status值为0 或者1，或者2，
    # 为0：管理员取消定单
    # 为1：订单正常处理
    # 为2：用户取得定单
    # 如查result_order_status有值且result_order_status.get('driver_id')无值，
    # 为0：管理员取消定单
    # 为2：用户取消定单
    result_dirver = {}
    if result_order_status.get('dirver_id'):

        with ConnectMysql() as c:
            sql__ = "SELECT * FROM dirver WHERE id=%s"
            result_dirver = c.select_one(sql__, (result_order_status.get('dirver_id'),))


    return {
        "status": 1,
        "errorCode": None,
        "errorMsg" : None,
        "resultBody": result,
        "resultBody": {
            'order': result, 
            'order_status': result_order_status,
            'dirver' : result_dirver
        }
       
    }